package cn.com.xyc.rabbitmq;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.SerializationUtils;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

public class QueueConsumer extends EndPoint implements 	Runnable,Consumer{

	public QueueConsumer(String endpointName) throws IOException {
		super(endpointName);
	}
	
	public void run() {
		try {
			channel.basicConsume(endPointName, true,this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void handleConsumeOk(String arg0) {
		System.out.println("Consumer " +arg0+" registered");
	}

	public void handleDelivery(String arg0, Envelope arg1,
			BasicProperties arg2, byte[] arg3) throws IOException {
		Map map=(HashMap)SerializationUtils.deserialize(arg3);
		System.out.println("Message Number "+map.get("message number")+" received.");
		
	}

	
	public void handleCancel(String arg0) throws IOException {
		// TODO Auto-generated method stub
		
	}

	public void handleCancelOk(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void handleRecoverOk(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void handleShutdownSignal(String arg0, ShutdownSignalException arg1) {
		// TODO Auto-generated method stub
		
	}

}
